package com.example.questionnairespringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.questionnairespringboot.Req.PersonReq;
import com.example.questionnairespringboot.entity.Tenant;
import com.example.questionnairespringboot.service.ITenantService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.oas.annotations.EnableOpenApi;

@EnableOpenApi
@RestController
@RequestMapping("/tenant")
public class TenantController {
    @Autowired
    private ITenantService tenantServiceImpl;
    //  登录
    @PostMapping("/login")
    public boolean login(@RequestBody PersonReq personReq){
        if (StringUtils.isBlank(personReq.getAccount())||StringUtils.isBlank(personReq.getPassword())){
            return false;
        }
        return tenantServiceImpl.login(personReq);
    }
    @PostMapping("/register")
    public boolean register(@RequestBody Tenant t) {
        System.out.println(t);
        return tenantServiceImpl.save(t);
    }
    @GetMapping("/page")
    public IPage<Tenant> findPage(@RequestParam Integer pageNum,
        @RequestParam Integer pageSize,@RequestParam(defaultValue = "") String account) {
        IPage<Tenant> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Tenant> queryWrapper = new QueryWrapper<>();
        if(!"".equals(account)){
            queryWrapper.like("account",account);
        }
        return tenantServiceImpl.page(page,queryWrapper);
    }
    @PostMapping("/save")
    public boolean save(@RequestBody Tenant t){
        return tenantServiceImpl.saveOrUpdate(t);
    }
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id){
        return tenantServiceImpl.removeById(id);
    }
    @PostMapping("/del/batch")
    public boolean delete(@RequestBody List<Integer> ids){
        return tenantServiceImpl.removeBatchByIds(ids);
    }
}
